package org.example.blog.services.impl;

import org.example.blog.entities.User;
import org.example.blog.services.UserService;
import org.zoomdev.zoom.dao.Dao;

import java.time.LocalDateTime;

public class UserServiceImpl implements UserService {

    private final Dao dao;

    public UserServiceImpl(Dao dao) {
        this.dao = dao;
    }

    // 伪代码 - 实际项目中注入DAO
    public User registerUser(String username, String email, String password) {
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setEmail(email);
        // 实际应用中应使用BCrypt加密
        newUser.setPasswordHash(password);
        newUser.setCreatedAt(LocalDateTime.now());
        newUser.setUpdatedAt(LocalDateTime.now());

        // 伪代码 - 保存到数据库
        // userRepository.save(newUser);
        dao.ar(User.class).insert(newUser);

        return newUser;
    }

    public User login(String email, String password) {
        // 伪代码 - 从数据库获取用户
        User user = dao.ar(User.class).where("email", email).get();

        // 实际应用中验证密码
        if (user != null && user.getPasswordHash().equals(password)) {
            return user;
        }
        return null;
    }

    public User getUserById(Long userId) {
        // 伪代码 - 从数据库获取用户
        return dao.ar(User.class).get(userId);
    }
}
